package com.springboot.framework.service.impl;

import com.springboot.framework.dao.entity.ProductCategory;
import com.springboot.framework.dao.mapper.ProductCategoryMapper;
import com.springboot.framework.enums.Errors;
import com.springboot.framework.enums.StatusEnum;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.CategoryModel;
import com.springboot.framework.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author SWF
 * @Date 2019/6/6 13:47
 **/
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public CategoryModel get(Integer categoryId) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(categoryId);
        return convertCategoryModelFromCategory(productCategory);
    }

    @Override
    public CategoryModel getByName(String name) {
        Example example = new Example(ProductCategory.class);
        example.and().andEqualTo("name" , name)
                .andNotEqualTo("status" , StatusEnum.DELETE.getCode());
        ProductCategory productCategory = productCategoryMapper.selectOneByExample(example);
        return convertCategoryModelFromCategory(productCategory);
    }

    /**
     * 查询所有正常品类
     * @return
     */
    @Override
    public List<CategoryModel> listNormal() {
        Example example = new Example(ProductCategory.class);
        example.and().andEqualTo("status" , StatusEnum.NORMAL.getCode());
        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(example);
        return convertCategoryModelFromCategory(productCategories);
    }

    /**
     * 查询所有品类
     * @return
     */
    @Override
    public List<CategoryModel> listAll() {
        Example example = new Example(ProductCategory.class);
        example.and().andNotEqualTo("status" , StatusEnum.DELETE.getCode());
        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(example);
        return convertCategoryModelFromCategory(productCategories);
    }

    @Override
    public CategoryModel insert(CategoryModel categoryModel) {
        if(this.getByName(categoryModel.getName()) != null){
            throw new BusinessException("品类已存在");
        }
        ProductCategory productCategory = convertProductCategoryFromCategoryModel(categoryModel);
        int resultCount = productCategoryMapper.insertSelective(productCategory);
        if(resultCount == 0){
            throw new BusinessException(Errors.SYSTEM_INSERT_FAIL);
        }
        return this.get(productCategory.getId());
    }



    private CategoryModel convertCategoryModelFromCategory(ProductCategory productCategory){
        if(productCategory == null){
            return null;
        }
        CategoryModel categoryModel = new CategoryModel();
        BeanUtils.copyProperties(productCategory,categoryModel);
        return categoryModel;
    }

    private List<CategoryModel> convertCategoryModelFromCategory(List<ProductCategory> productCategories){
        if(productCategories == null){
            return null;
        }
        return productCategories.stream()
                .map( e -> convertCategoryModelFromCategory(e))
                .collect(Collectors.toList());
    }

    private ProductCategory convertProductCategoryFromCategoryModel(CategoryModel categoryModel){
        if(categoryModel == null){
            return null;
        }
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(categoryModel,productCategory);
        return productCategory;
    }
}
